home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / labs / feedback / feedback.c next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.4 KB  |  274 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* feedback.c
  19.  *    This program demonstrates use of OpenGL feedback.  First,
  20.  *     a lighting environment is set up and a few primitives are 
  21.  *     drawn. Then feedback mode is entered, and the same 
  22.  *     primitives are drawn.  The results in the feedback buffer 
  23.  *     are printed.
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <stdio.h>
  30.  
  31. /* Function Prototypes */
  32.  
  33. GLvoid initgfx( GLvoid );
  34. GLvoid drawScene( GLvoid );
  35. GLvoid reshape( GLsizei, GLsizei );
  36. GLvoid keyboard( GLubyte, GLint, GLint );
  37.  
  38. GLvoid drawGeometry( GLboolean );
  39.  
  40. GLvoid print3DcolorVertex( GLint *, GLfloat *, GLboolean );
  41. GLvoid printBuffer( GLint, GLfloat * );
  42. GLvoid printHits( GLint, GLuint [] );
  43.  
  44. void printHelp( char * );
  45.  
  46. /* Global Definitions */
  47.  
  48. #define KEY_ESC    27    /* ascii value for the escape key */
  49.  
  50. #define BUFSIZE    1024
  51.  
  52. /* Global Variables */
  53.  
  54. GLboolean feedbackFlag = GL_FALSE;
  55.  
  56. void
  57. main( int argc, char *argv[] )
  58. {
  59.     GLsizei width, height;
  60.  
  61.     glutInit( &argc, argv );
  62.  
  63.     /* create a window that is 1/4 the size of the screen */
  64.  
  65.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  66.     height = glutGet( GLUT_SCREEN_HEIGHT );
  67.     glutInitWindowPosition( width / 4, height / 4 );
  68.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  69.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  70.     glutCreateWindow( argv[0] );
  71.  
  72.     initgfx();
  73.  
  74.     glutKeyboardFunc( keyboard );
  75.     glutReshapeFunc( reshape );
  76.     glutDisplayFunc( drawScene ); 
  77.  
  78.     printHelp( argv[0] );
  79.  
  80.     glutMainLoop();
  81. }
  82.  
  83. GLvoid
  84. printHelp( char *progname )
  85. {
  86.     fprintf(stdout, "\n%s - This program demonstrates feedback \n" 
  87.         "SPACE key            - toggle feedback mode \n"
  88.         "Escape key            - exit the program \n\n",
  89.         progname );
  90. }
  91.  
  92.  
  93. GLvoid
  94. initgfx( void )
  95. {
  96.     GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
  97.     GLfloat mat_diffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  98.     GLfloat light_position[] = { 0.0, 0.707, 0.707, 0.0 };
  99.  
  100.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  101.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  102.     glLightfv( GL_LIGHT0, GL_POSITION, light_position);
  103.     glEnable( GL_LIGHTING );
  104.     glEnable( GL_LIGHT0 );
  105.  
  106.     glClearColor( 0, 0, 0, 1 );
  107.     glEnable( GL_DEPTH_TEST );
  108. }
  109.  
  110. GLvoid
  111. reshape( GLsizei width, GLsizei height )
  112. {
  113.     GLdouble    aspect;
  114.  
  115.     glViewport( 0, 0, width, height );
  116.  
  117.     glMatrixMode( GL_PROJECTION );
  118.     glLoadIdentity();
  119.     aspect = (GLdouble) width / (GLdouble) height;
  120.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  121.     glMatrixMode( GL_MODELVIEW );
  122. }
  123.  
  124. GLvoid 
  125. keyboard( GLubyte key, GLint x, GLint y )
  126. {
  127.     switch (key) {
  128.     case ' ':     /* toggle feedback when the SPACE BAR is pressed */
  129.         feedbackFlag = !feedbackFlag;
  130.         glutPostRedisplay();
  131.         break;
  132.         
  133.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  134.         exit(0);
  135.     }
  136. }
  137.  
  138.  
  139. /* Draw a few lines and two points, one of which will be clipped. If in 
  140.  * feedback mode, a passthrough token is issued between each object.
  141.  */
  142. GLvoid 
  143. drawGeometry( GLboolean feedback )
  144. {
  145.     glBegin( GL_LINE_STRIP );
  146.         glNormal3f( 0.0, 0.0, 1.0 );
  147.         glVertex3f( 3.0, 3.0, -5.0 );
  148.         glVertex3f( 3.0, 6.0, -6.0 );
  149.         glVertex3f( 0.0, 4.0, -10.0 );
  150.     glEnd();
  151.  
  152.     if (feedback) glPassThrough( 1.0 );
  153.  
  154.     glBegin( GL_POINTS );
  155.         glVertex3f( -100.0, -100.0, -100.0 );    /*  clipped  */
  156.         glVertex3f( 2.0, 2.0, -100.0 );
  157.     glEnd();
  158.  
  159.     if (feedback) glPassThrough( 2.0 );
  160.  
  161.     glBegin( GL_POINTS );
  162.         glNormal3f( 0.0, 0.0, 1.0 );
  163.         glVertex3f( -2.0, -2.0, -2.0 );
  164.     glEnd();
  165.  
  166.     if (feedback) glPassThrough( 3.0 );
  167.  
  168.     glutSolidCube( 1.0 );
  169.  
  170.     if (feedback) glPassThrough( 4.0 );
  171.  
  172.     glTranslatef( 2.0, 0.0, -1.0 );
  173.     glutSolidTetrahedron();
  174. }
  175.  
  176. GLvoid  
  177. drawScene( GLvoid )
  178. {
  179.     GLfloat feedBuffer[BUFSIZE];
  180.     GLint size;
  181.  
  182.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  183.     glPushMatrix();
  184.         glTranslatef( 0.0, 0.0, -10.0 );
  185.  
  186.         drawGeometry( GL_FALSE );
  187.     
  188.         if ( feedbackFlag )
  189.         {   
  190.             glFeedbackBuffer( BUFSIZE, GL_3D_COLOR, feedBuffer );
  191.             (void) glRenderMode( GL_FEEDBACK );
  192.  
  193.             drawGeometry( GL_TRUE );
  194.  
  195.             size = glRenderMode( GL_RENDER );
  196.             printBuffer( size, feedBuffer );
  197.         }
  198.     glPopMatrix();
  199.     glutSwapBuffers();
  200. }
  201.  
  202. GLvoid
  203. printBuffer( GLint size, GLfloat *buffer )
  204. {
  205.     GLint count = 0, i;
  206.     GLuint token, nVertices;
  207.     
  208.     while (count < size)
  209.     {
  210.         token = (int) buffer[count++];
  211.         switch ( token ) {
  212.         case GL_PASS_THROUGH_TOKEN:
  213.             printf( "\nGL_PASS_THROUGH_TOKEN\t" );
  214.             printf( "  %4.2f\n", buffer[count++] );
  215.             break;
  216.         case GL_POINT_TOKEN:
  217.             printf( "\nGL_POINT_TOKEN\n" );
  218.             print3DcolorVertex( &count, buffer, GL_TRUE );
  219.             break;
  220.         case GL_LINE_TOKEN:
  221.             printf( "\nGL_LINE_TOKEN\n" );
  222.             print3DcolorVertex( &count, buffer, GL_TRUE );
  223.             print3DcolorVertex( &count, buffer, GL_FALSE );
  224.             break;
  225.         case GL_LINE_RESET_TOKEN:
  226.             printf( "\nGL_LINE_RESET_TOKEN\n" );
  227.             print3DcolorVertex( &count, buffer, GL_TRUE );
  228.             print3DcolorVertex( &count, buffer, GL_FALSE );
  229.             break;
  230.         case GL_BITMAP_TOKEN:
  231.             printf( "\nGL_BITMAP_TOKEN\n" );
  232.             print3DcolorVertex( &count, buffer, GL_TRUE );
  233.             break;
  234.         case GL_DRAW_PIXEL_TOKEN:
  235.             printf( "\nGL_DRAW_PIXEL_TOKEN\n" );
  236.             print3DcolorVertex( &count, buffer, GL_TRUE );
  237.             break;
  238.         case GL_COPY_PIXEL_TOKEN:
  239.             printf( "\nGL_COPY_PIXEL_TOKEN\n" );
  240.             print3DcolorVertex( &count, buffer, GL_TRUE );
  241.             break;
  242.         case GL_POLYGON_TOKEN:
  243.             printf( "\nGL_POLYGON_TOKEN  " );
  244.             nVertices = (GLuint) buffer[count++];
  245.             printf( "numVertices =  %d\n", nVertices );
  246.             for ( i = 0; i < nVertices; i++ )
  247.             {
  248.                 print3DcolorVertex( &count, buffer, (i==0));
  249.             }
  250.             break;
  251.         default:
  252.             printf( "Unrecognized token\n" );
  253.             break;
  254.         }
  255.     }
  256.     printf( "\nEND FRAME\n\n" );
  257. }
  258.  
  259.  
  260. GLvoid
  261. print3DcolorVertex( GLint *count, GLfloat *buffer, GLboolean printHeader )
  262. {
  263.     int i;
  264.  
  265.     if ( printHeader )
  266.         fprintf( stdout, "   X      Y    Z    R    G    B    A\n" );
  267.  
  268.     for (i = 0; i < 7; i++) 
  269.     {
  270.         fprintf( stdout, "%4.2f ", buffer[(*count)++] );
  271.     }
  272.     fprintf( stdout, "\n" );
  273. }
  274.